package com.wish.building.controller;



import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.wish.building.entity.SysRegion;
import com.wish.building.service.ISysRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author 高锦涛
 * @since 2022-03-23
 */
@RestController
public class SysRegionController {
    @Autowired
    ISysRegionService iSysRegionService;


    @GetMapping("region")
    public List<Tree<Integer>> region(){
        List<SysRegion> list = iSysRegionService.list();

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setParentIdKey("parentId");

        List<Tree<Integer>> build = TreeUtil.build(list, 1, treeNodeConfig, (tree,treeNode) -> {
            treeNode.setId(tree.getRegionId());
            treeNode.setName(tree.getRegionName());
            treeNode.setParentId(tree.getParentId());

            //tree.putExtra("value", treeNode.getRegionId());
           // tree.putExtra("label", treeNode.getRegionName());
        });

        return build;

    }

    //树桩图
    @GetMapping("dq")
    private List<SysRegion> dq(){
        List<SysRegion> list = iSysRegionService.dq();
        return list;
    }


}
